package com.gvfish.backingbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.gvfish.core.Action;
import com.gvfish.entity.Category;
import com.gvfish.service.CategoryManager;

@ManagedBean
@RequestScoped
public class CategoryBean {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Category> categorys;
	private Category category = new Category();
	private boolean editModel;
	ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
	CategoryManager categoryManager = (CategoryManager) ctx.getBean("categoryManager");

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~constructor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	public CategoryBean() {
	}

	private DataModel<Category> categoryModel = new ListDataModel<Category>();
	{
		categoryModel.setWrappedData(getCategorys());
	}

	public DataModel<Category> getAllCategory() {
		return categoryModel;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~action~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	/**
	 * 增加栏目
	 */
	public String add() {
		category = new Category();
		return "success";
	}

	/**
	 * 删除栏目
	 */
	public void del() {
		category = categoryModel.getRowData();
		categoryManager.del(category.getId());
		categorys = categoryManager.findAll();
		categoryModel.setWrappedData(getCategorys());
	}

	public String edit() {
		System.out.println("in edit");
		setEditModel(true);
		category = categoryModel.getRowData();
		category = categoryManager.find(category.getId());
		return Action.SUCCESS;
	}

	public String save() {
		System.out.println("in save" + category);
		categoryManager.save(category, null);
		categoryModel.setWrappedData(categoryManager.findAll());
		category = new Category();
		return Action.SUCCESS;
	}

	public String edit2() {
		System.out.println("in edit2");
		System.out.println(category);
		categoryManager.update(category);
		categoryModel.setWrappedData(categoryManager.findAll());
		return Action.SUCCESS;
	}

	public List<SelectItem> getSelectItemList() {
		List<SelectItem> selectItemList = new ArrayList<SelectItem>();
		SelectItem top = new SelectItem("", "无，作为顶级栏目");
		selectItemList.add(top);
		for (Category category : getCategorys()) {
			// selectItemList.add(new SelectItem(category.getId(),
			// category.getName()));
			selectItemList.add(new SelectItem(category.getId(), category.getName()));
		}
		return selectItemList;
	}

	public void delAllArt(ActionEvent event) {
//		ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
//		CategoryManager categoryManager = (CategoryManager) ctx.getBean("categoryManager");
//		FacesContext ctx = FacesContext.getCurrentInstance();
//		Long id = Long.parseLong((String) ctx.getExternalContext().getRequestParameterMap().get("id"));
//		System.out.println(id);
//		System.out.println("in delAllArt");
//		categoryManager.delAllArt(id);
//		categorys = categoryManager.findAll();
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~set,get
	// model~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	public void setCategory(Category category) {
		this.category = category;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategorys(List<Category> categorys) {
		this.categorys = categorys;
	}

	public List<Category> getCategorys() {
		if (categorys == null) {
			categorys = categoryManager.findAll();
		}
		return categorys;
	}

	public void setEditModel(boolean editModel) {
		this.editModel = editModel;
	}

	public boolean isEditModel() {
		return editModel;
	}

}
